package com.niiwoo.civet.trade.service.dubbo.loan;

import com.alibaba.dubbo.config.annotation.Reference;
import com.niiwoo.civet.trade.dto.request.FastLoanProjectDTO;
import com.niiwoo.civet.trade.dto.request.loan.PushProjectRequestDTO;
import com.niiwoo.civet.trade.enums.ProjectStatusEnum;
import com.niiwoo.civet.trade.enums.ProjectSubStatusEnum;
import com.niiwoo.civet.trade.enums.ProjectTypeEnum;
import com.niiwoo.civet.trade.service.loan.FastLoanDubboService;
import com.niiwoo.civet.trade.service.loan.SpeedLoanDubboService;
import com.niiwoo.civet.trade.service.local.project.ProjectLocalService;
import com.niiwoo.civet.user.dto.response.UserBasicInfoResponseDTO;
import com.niiwoo.civet.user.service.UserBasicInfoDubboService;
import com.niiwoo.tripod.provider.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * Created by lujing on 2018/5/17.
 */

public class FastLoanDubboServiceImpl implements FastLoanDubboService {

    @Autowired
    private ProjectLocalService projectLocalService;
    @Reference(version = "1.0.0")
    private UserBasicInfoDubboService userBasicInfoDubboService;
    @Reference(version = "1.0.0")
    private SpeedLoanDubboService speedLoanDubboService;

    @Override
    public void fastLoan(FastLoanProjectDTO fastLoanProjectDTO) {
        Long projectId = null;
        if (fastLoanProjectDTO.getType().equals(ProjectTypeEnum.ORG_SPEEDLOAN_NEW.getValue())) {
            UserBasicInfoResponseDTO userBasicInfoResponseDTO = userBasicInfoDubboService.selectUserBaseInfo(fastLoanProjectDTO.getUserId());
            if (userBasicInfoResponseDTO == null) {
                throw new BizException("");
            }
            fastLoanProjectDTO.setUserName(userBasicInfoResponseDTO.getNickName());
            fastLoanProjectDTO.setMobile(userBasicInfoResponseDTO.getMobileNo());
            fastLoanProjectDTO.setHeadImage(userBasicInfoResponseDTO.getHeadImage());
            fastLoanProjectDTO.setNickname(userBasicInfoResponseDTO.getNickName());
            fastLoanProjectDTO.setGender(userBasicInfoResponseDTO.getGender());
            fastLoanProjectDTO.setStatus(ProjectStatusEnum.CHECKING.getStatus());
            fastLoanProjectDTO.setSubStatus(ProjectSubStatusEnum.CHECKING_MANUAL_LAST.getSubStatus());
            projectId = projectLocalService.fastLoanProject(fastLoanProjectDTO);
        }


        PushProjectRequestDTO pushProjectRequestDTO = new PushProjectRequestDTO();
        pushProjectRequestDTO.setProjectId(projectId);
        pushProjectRequestDTO.setAuditedTime(new Date());
        pushProjectRequestDTO.setAuditManagerId(-1L);
        pushProjectRequestDTO.setPushFromEnum(PushProjectRequestDTO.PushFromEnum.HAND_AUDIT);
        speedLoanDubboService.pushProject(pushProjectRequestDTO);
    }
}
